package org.ragdollcat.secondaiagent.app;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import org.ragdollcat.secondaiagent.model.image.Content;
import org.ragdollcat.secondaiagent.model.image.ImageInputModel;
import org.ragdollcat.secondaiagent.model.image.ImageUrl;
import org.ragdollcat.secondaiagent.model.image.Message;


import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class DashScopeHutoolClient {
    private static final String API_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";

    // URL验证正则表达式
    private static final String URL_REGEX = "^(http|https)://.*$";
    private static final Pattern URL_PATTERN = Pattern.compile(URL_REGEX);



    public String sendRequest() {
        // 图片URL - 使用一个已知有效的公开图片URL
        String imageUrlStr = "https://tiebapic.baidu.com/forum/pic/item/9595a819972bd407948bbac43d899e510eb309e9.jpg?tbpicau=2025-10-11-05_3ab3423fb12f94bda641bd14b01e754e";
//        String imageUrlStr = "https://img1.baidu.com/it/u=1991159576,3929042115&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1200";

        // 验证URL格式
//        if (!isValidUrl(imageUrlStr)) {
//            System.err.println("图片URL格式无效，使用备用URL");
//            imageUrlStr = "https://picsum.photos/800/600?random=1";
//        }

        // 获取图片的Content-Length
        long contentLength = getContentLength(imageUrlStr);
        if (contentLength <= 0) {
            System.err.println("无法获取图片大小，使用默认值");
            contentLength = 1024 * 1024; // 1MB默认值
        }

        // 创建图片内容，包含URL和Content-Length
        ImageUrl imageUrl = new ImageUrl(imageUrlStr);
        Content imageContent = new Content("image_url", imageUrl);

        // 创建文本内容
        Content textContent = new Content("text", "图上的妹妹太漂亮了");

        // 组合内容列表
        List<Content> contents = new ArrayList<>();
        contents.add(imageContent);
        contents.add(textContent);

        // 创建消息
        Message message = new Message("user", contents);
        List<Message> messages = new ArrayList<>();
        messages.add(message);

        // 创建请求对象
        ImageInputModel requestBody = new ImageInputModel("qwen-vl-plus", messages);

        // 转换为JSON字符串并打印
        String jsonBody = JSONUtil.toJsonStr(requestBody);
        System.out.println("请求JSON: " + jsonBody);

        // 使用Hutool发送POST请求
        String result = HttpRequest.post(API_URL)
                .header("Authorization", "Bearer " + "sk-1bd47c8ea92a4b9799ee0afc9373d393")
                .header("Content-Type", "application/json")
                .header("Content-Length", String.valueOf(contentLength))
                .body(jsonBody)
                .execute()
                .body();

        return result;
    }

    // 获取图片的Content-Length
    private long getContentLength(String url) {
        try (HttpResponse response = HttpRequest.head(url).execute()) {
            return response.contentLength();
        } catch (Exception e) {
            System.err.println("获取图片大小失败: " + e.getMessage());
            return -1;
        }
    }


    public static void main(String[] args) {

        DashScopeHutoolClient client = new DashScopeHutoolClient();
        String response = client.sendRequest();
        System.out.println("API响应: " + response);
    }
}
